﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataGrid导入导出.Base;
using DataGrid导入导出.Dialog;
using OfficeOpenXml;
using LicenseContext = OfficeOpenXml.LicenseContext;

namespace DataGrid导入导出
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<string> Layers { get; set; } = new List<string>();
        public ObservableCollection<User> UserList { get; set; } = new ObservableCollection<User>();
        public ICommand ExportToPrintCmd { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            InitLayers();
            this.ly.ItemsSource = Layers;
            InitUserList();

            ExportToPrintCmd = new BaseCommand(ExportToPrint);

            this.DataContext = this;
        }

        private void InitLayers()
        {
            Layers.Add("员工");
            Layers.Add("组长");
            Layers.Add("经理");
            Layers.Add("主管");
        }

        private void InitUserList()
        {
            Random random = new Random();

            for (int i = 0; i < 100; i++)
            {
                int age = random.Next(1, 120);
                string layer = Layers[random.Next(Layers.Count)];
                UserList.Add(new User() { ID = i, Name = "User:" + i, Age = age, Avatar = "Resources/katong2.png", Address = "山东省济宁市金乡县", Layer = layer, State = i % 2 });
            }
        }

        private void ExportToExcel_Click(object sender, RoutedEventArgs e)
        {
            // 导出到 Excel 的逻辑
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            var filePath = "DataGridExport.xlsx";

            using (var package = new ExcelPackage())
            {
                var worksheet = package.Workbook.Worksheets.Add("Sheet1");

                // 写入列头
                for (int i = 0; i < dg.Columns.Count - 1; i++)
                {
                    worksheet.Cells[1, i + 1].Value = dg.Columns[i].Header.ToString();
                }

                // 写入内容列表
                // 对应顺序
                List<string> columnOrders = new List<string>() { "ID", "Name", "Avatar", "Age", "Address", "State", "Layer" };
                System.Reflection.PropertyInfo[] propertyInfos = typeof(User).GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                Dictionary<string,PropertyInfo> propDic = new Dictionary<string, PropertyInfo>();
                for (int j = 0; j < columnOrders.Count; j++)
                {
                    PropertyInfo prop = propertyInfos.First(p => p.Name == columnOrders[j]);
                    if(prop!= null)
                        propDic.Add(prop.Name, prop);
                }

                for (int i = 0; i < UserList.Count; i++)
                {
                    User user = UserList[i];
                    for (int j = 0; j < columnOrders.Count; j++)
                    {
                        object? val = propDic[columnOrders[j]].GetValue(user);
                        worksheet.Cells[i + 2,j + 1].Value = val;
                    }
                }

                // 保存文件
                var fileInfo = new FileInfo(filePath);
                package.SaveAs(fileInfo);
            }

            MessageBox.Show($"Data exported to {filePath}");
        }

        private void ExportToPrint(object param)
        {
            DataGrid? dataGrid = param as DataGrid;
            new PrintViewDialog(dataGrid).ShowDialog();
        }
    }

    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Avatar { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public int State { get; set; }
        public string Layer { get; set; }
    }
}